import string
import urllib.request
from urllib.parse import quote
from bs4 import BeautifulSoup

def nospace(word):
    return word.strip().replace("\n", "")

city = input("请输入城市：")
code = {
    '北京': '530',
    '上海': '538',
    '广州': '763',
    '深圳': '765',
    '天津': '531',
    '武汉': '736',
    '西安': '854',
    '成都': '801',
    '沈阳': '599',
    '南京': '635',
    '杭州': '653',
    '苏州': '639',
    '重庆': '551',
    '长沙': '749',
    '厦门': '682',
    '南昌': '691'
}
city_code = code.get(city)
if not city_code:
    print("未找到该城市的代码，请重新输入。")
else:
    jobName = input("请输入相关岗位：")
    # 仅对 jobName 进行编码
    encoded_jobName = quote(jobName)
    url = f"https://sou.zhaopin.com/?jl={city_code}&kw={encoded_jobName}&p=1"
    # 设置请求头，模拟浏览器访问
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
    req = urllib.request.Request(url, headers=headers)
    try:
        resp = urllib.request.urlopen(req)
        content = resp.read().decode('utf-8')
        soup = BeautifulSoup(content, "lxml")
        jobList = soup.select(".joblist-box__item")
        job_list = []
        for job in jobList:
            job_dic = {}
            job_name = job.select_one(".jobinfo__name").get_text()
            job_sal = job.select_one(".jobinfo__salary").get_text()
            job_sal = nospace(job_sal)
            if '天' in job_sal:
                try:
                    min_sal, max_sal = map(int, job_sal.split("-"))
                    min_sal = min_sal * 30
                    max_sal = max_sal * 30
                    job_sal = f"{min_sal}-{max_sal}"
                except ValueError:
                    job_sal = 5000
            elif '千' in job_sal:
                try:
                    min_sal, max_sal = map(float, job_sal.replace("千", "").split("-"))
                    min_sal = int(min_sal * 1000)
                    max_sal = int(max_sal * 1000)
                    job_sal = f"{min_sal}-{max_sal}"
                except ValueError:
                    job_sal = 5000
            elif '万' in job_sal:
                try:
                    min_sal, max_sal = map(float, job_sal.replace("万", "").split("-"))
                    min_sal = int(min_sal * 10000)
                    max_sal = int(max_sal * 10000)
                    job_sal = f"{min_sal}-{max_sal}"
                except ValueError:
                    job_sal = 5000
            else:
                job_sal = 5000
            company_name = nospace(job.select_one(".companyinfo__name").get_text())
            job_msg = job.select(".jobinfo__other-info-item")
            job_place = nospace(job_msg[0].get_text())
            job_exp = nospace(job_msg[1].get_text())
            job_education = nospace(job_msg[2].get_text())
            job_dic['job_name'] = job_name
            job_dic['job_sal'] = job_sal
            job_dic['company_name'] = company_name
            job_dic['job_place'] = job_place[0:2]
            job_dic['job_exp'] = job_exp
            job_dic['job_education'] = job_education
            job_list.append(job_dic)
        for i in job_list:
            print(i)
    except urllib.error.HTTPError as e:
        print(f"HTTP 错误: {e.code} {e.reason}")
    except urllib.error.URLError as e:
        print(f"URL 错误: {e.reason}")
    except Exception as e:
        print(f"发生未知错误: {e}")
